-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shim Apple's futex primitives #4142
base: master
Are you sure you want to change the base?
Conversation
Thanks for the PR! I just left for a 1-week business trip and will have to catch up with stuff when I am back so it could take a bit before I can take a closer look. |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Overall approach looks great, but I have a bunch of questions and comments. :)
fn wake_nobody() { | ||
let futex = 0; | ||
|
||
// Wake 1 waiter. Expect zero waiters woken up, as nobody is waiting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Wake 1 waiter. Expect zero waiters woken up, as nobody is waiting. | |
// Wake 1 waiter. Expect ENOENT, as nobody is waiting. |
size_of::<i32>(), | ||
libc::OS_SYNC_WAIT_ON_ADDRESS_NONE, | ||
), | ||
0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test where wait_on_address returns something non-zero (since there are more waiters).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While doing this, I noticed that the reported count was incorrect if os_sync_wake_by_address_all
was used as the threads were woken one-by-one. dfa8d2fa13d2c9972208ab7dbc62e197a44f1920 fixes this (and also gets rid of the quadratic runtime of wakeup 😄).
This is necessary to unblock rust-lang/rust#122408. The documentation for these is available [here](https://developer.apple.com/documentation/os/os_sync_wait_on_address?language=objc). Because the futex wait operations (`os_sync_wait_on_address` et al.) return the number of remaining waiters after returning, this required some changes to the common futex infrastructure, which I've changed to take a callback instead of precalculating the return values.
On macOS, the futex operations return the number of outstanding waiters. For this count to be correct, we have to remove all eligible threads from the internal futex list before any of the wakeup callbacks are run. This has the nice side-effect of making the runtime removal operation linear in the number of threads instead of quadratic.
This is necessary to unblock rust-lang/rust#122408. The documentation for these is available here.
Because the futex wait operations (
os_sync_wait_on_address
et al.) return the number of remaining waiters after returning, this required some changes to the common futex infrastructure, which I've changed to take a callback instead of precalculating the return values.